package com.hy.z.exception;

import com.hy.z.result.ReInter;
import com.hy.z.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@ControllerAdvice
@RestControllerAdvice
@ResponseBody
public class AllExceptionHandler {

    //统一异常处理.
    @ExceptionHandler(Exception.class)
    public Result handlerException(GlobalException e) {
        ReInter reInter = e.getReInter();
        log.error(reInter.msg(),e);
        return Result.error(reInter);
    }

    //认证异常单独处理
    @ExceptionHandler(AuthorizationException.class)
    public Map<String, Object> handlerAuthorizationException() {
        //log.error("异常信息",e);
        Map<String, Object> m = new HashMap<>();
        m.put("code", 403);
        m.put("message", "你没有操作权限,请联系管理员");
        return m;
    }
}
